import { createStore } from 'vuex'
import api from '@/api/index.js'

export default createStore({
  state: {
    playlist: [{
      name: '雅俗共赏',
      id: 411214279,
      al: {
        id: 34749138,
        name: "青年晚报",
        pic: 3431575794705764,
        picUrl: "http://p4.music.126.net/Wcs2dbukFx3TUWkRuxVCpw==/3431575794705764.jpg",
      }
    }],
    playCurrentIndex: 0,
    lyric: '',
    currentTime:0,
    intervalId:0,
    user:{
      isLogin:false,
      // username:'未登录',
      account:{},
      userDetail:{}
    }
  },
  getters: {
    lyriclist: function (state) {
      console.log(state.lyric);
      let arr = state.lyric.split(/\n/gis).map((item,i,arr) => {
        let min = parseInt(item.slice(1, 3)) 
        let sec = parseInt(item.slice(4,6))
        let mill = parseInt(item.slice(7,10))
        return {
          min, sec, mill, 
          lyric: item.slice(11, item.length),
          content:item,
          time:mill+sec*1000+min*60*1000
        }
      })
      // console.log(arr);
      arr.forEach((item,i)=>{
        // console.log(i);
        if(i-2 < 0){
          item.pre = 0
          // console.log(item.pre);
        }else{
          item.pre = arr[i-1].time
          // console.log(item.pre);
        }
      })
      return arr
    }
  },
  mutations: {
    setPlaylist: function (state, value) {
      state.playlist = value
    },
    pushPlaylist: function (state, value) {
      state.playlist.push(value)
    },
    setPlayIndex(state, value) {
      state.playCurrentIndex = value
    },
    setLyric(state, value) {
      state.lyric = value
    },
    setCurrentTime(state,value) {
      state.currentTime = value
    },
    setUser(state,value){
      state.user = value
    },
  },
  actions: {
    async reqLyric(content, payload) {
      // console.log(payload.id);
      let result = await api.getLyric(payload.id)
      content.commit('setLyric', result.data.lrc.lyric)
      // console.log(result.data.lrc.lyric);
    },
    async phoneLogin(content,payload){
      // console.log(payload);
      let result = await api.phoneLogin(payload.phone,payload.password)
      // console.log(result.data.code);
      if(result.data.code == 200){
        content.state.user.isLogin = true
        content.state.user.account = result.data.account
        let userDetail = await api.userDetail(result.data.account.id)
        // console.log(content.state.user.account);
        content.state.user.userDetail = userDetail.data
        localStorage.userData = JSON.stringify(content.state.user)
        content.commit('setUser',content.state.user)
        console.log(userDetail);
    }
      console.log(result);
      return result
    }
  },
  modules: {
  }
})
